/*
 * DOMCharactersProxy.java
 *
 * Created on September 4, 2006, 1:05 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package org.infoset.xml.dom;

import org.infoset.xml.Characters;
import org.infoset.xml.Text;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;

/**
 *
 * @author alex
 */
public class DOMCharactersProxy extends DOMChildProxy implements org.w3c.dom.Text {
   
   /** Creates a new instance of DOMCharactersProxy */
   public DOMCharactersProxy(Characters item,DOMDocumentProxy docProxy) {
      super(item,docProxy);
   }
   
   public short getNodeType() {
      return Node.TEXT_NODE;
   }
   
   public String getNodeValue() {
      return getTextContent();
   }
   
   public void setNodeValue(String value) {
      setTextContent(value);
   }
   
   public String getNodeName() {
      return "#text";
   }
   
   public void setTextContent(String value) {
      Text t = (Text)item;
      t.setText(value);
   }
   
   public String getTextContent() {
      Text t = (Text)item;
      return t.getText();
   }
   
   public Node cloneNode(boolean deep) {
      return new DOMCharactersProxy((Characters)item.copyOfItem(deep),documentProxy);
   }
   
   public org.w3c.dom.Text replaceWholeText(String content) {
      setTextContent(content);
      return this;
   }
   
   public String getWholeText() {
      return getTextContent();
   }
   
   public boolean isElementContentWhitespace() {
      String text = getTextContent();
      int len = text.length();
      for (int i=0; i<len; i++) {
         if (!Character.isWhitespace(text.charAt(i))) {
            return false;
         }
      }
      return true;
   }
   public org.w3c.dom.Text splitText(int offset) {
      throw new DOMException(DOMException.NOT_SUPPORTED_ERR,"Splitting text nodes not supported.");
   }
   
   public void replaceData(int offset, int count, String arg) {
      String text = getTextContent();
      text = text.substring(0,offset)+arg+text.substring(offset+count);
      setTextContent(text);
   }
   public void deleteData(int offset, int count) {
      String text = getTextContent();
      text = text.substring(0,offset)+text.substring(offset+count);
      setTextContent(text);
   }
   public void insertData(int offset, String arg) {
      String text = getTextContent();
      text = text.substring(0,offset)+arg+text.substring(offset);
      setTextContent(text);
   }
   public void appendData(String arg) {
      String text = getTextContent();
      text = text+arg;
      setTextContent(text);
   }
   public String substringData(int offset,int count) {
      String text = getTextContent();
      return text.substring(offset,offset+count);
   }

   public int getLength() {
      return getTextContent().length();
   }
   
   public void setData(String text) {
      setTextContent(text);
   } 
   
   public String getData() {
      return getTextContent();
   }
}
